﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Icoders.MarketTracker.Core.MasterData;
using Icoders.MarketTracker.Core.Repository.MasterDataRepositories;
using Icoders.MarketTracker.Core.Validation;
using Icoders.MarketTracker.Web.Lib.Security;
using Icoders.MarketTracker.Web.Lib.Services;
using Icoders.MarketTracker.Web.Lib.Validation;

namespace Icoders.MarketTracker.Web.Areas.SmsAdmin.Controllers
{
    public class ResetController : Controller
    {
        //
        // GET: /SmsAdmin/Reset/
        private IUserRepository _userRepository;
        private IDropDownService _dropDownService;
        private IAuditTrailRepository _auditTrailRepository;
        CodersEncrypt passEncrypt = new CodersEncrypt();

        public ResetController(IUserRepository userRepository, IDropDownService dropDownService, IAuditTrailRepository auditTrailRepository)
        {
            _userRepository = userRepository;
            _dropDownService = dropDownService;
            _auditTrailRepository = auditTrailRepository;
        }

        public ActionResult Index()
        {
            return View(_userRepository.GetAll());
        }
        public ActionResult Reset(int id)
        {
            User user = _userRepository.GetById(id);
            return View(user);
        }
        [HttpPost]
        public ActionResult Reset(User model)
        {
            try
            {
                User user = _userRepository.GetById(model.Id);
                user.Password = passEncrypt.EncryptThings("1234");
                user.ConfirmPassword = passEncrypt.EncryptThings("1234");
                user.Enabled = true;
                user.ChangePass = true;
                _userRepository.Save(user);
                Saveaudit("Edited User", HttpContext.User.Identity.Name, ActionType.Updated);
            }
            catch (DomainValidationException dve)
            {
                ValidationSummary.DisplayDomainValidationResult(dve, ModelState);
                return View(model);
            }
            catch (Exception ex)
            {
                ValidationSummary.DisplayValidationResult(ex.Message, ModelState);
                return View(model);
            }
            return RedirectToAction("Index");
        }
        public void Saveaudit(string description, string username, ActionType actionType)
        {
            //  private IUserRepository _userRepository;
            //private IAuditTrailRepository _auditTrailRepository;
            // Saveaudit("Changed Password", HttpContext.User.Identity.Name, ActionType.Updated);
            try
            {
                User user = _userRepository.GetByUsername(username);
                AuditTrail auditTrail = new AuditTrail();
                auditTrail.Description = description;
                auditTrail.UserId = user.Id;
                auditTrail.ActionTypeId = Convert.ToInt32(actionType);
                _auditTrailRepository.Save(auditTrail);
            }
            catch (Exception)
            {
                throw;
            }
        }

    }
}
